using System;
using System.Reflection;

namespace SiliverlightJavascriptIntelli
{
    public class SilverlightParameter : IJavascriptConvertable
    {
        private string name;
        private string type;
        private bool isInteger;

        public SilverlightParameter(SilverlightMethod belongsTo)
        {
            this.belongsTo = belongsTo;
        }

        private SilverlightMethod belongsTo;
        private string descritpion;

        public SilverlightParameter(string Name, string ParameterType, string Descritpion)
        {
            this.BelongsTo = belongsTo;
            this.Name = Name;
            this.Type = ParameterType;
            this.IsInteger = TypeHelper.IsTypeInteger(ParameterType);
            this.Descritpion = Descritpion;
        }

        public static SilverlightParameter ConvertParameterInfoToSilverlightParameter(ParameterInfo curParameterInfoToConvertToSilverlightParam, SilverlightMethod methodToAddTo)
        {
            SilverlightParameter newParam = new SilverlightParameter(methodToAddTo);

            newParam.Name = curParameterInfoToConvertToSilverlightParam.Name;
            newParam.Type = TypeHelper.ConvertTypeToString(curParameterInfoToConvertToSilverlightParam.ParameterType);
            newParam.IsInteger = TypeHelper.IsTypeInteger(curParameterInfoToConvertToSilverlightParam.ParameterType);
            newParam.Descritpion =
                ReferenceAnalyzer.GetDescriptionForParam(newParam.BelongsTo.BelongTo.Name, newParam.BelongsTo.Name,
                                                         newParam.Name);


            return newParam;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }

        public bool IsInteger
        {
            get
            {
                return isInteger;
            }
            set
            {
                isInteger = value;
            }
        }

        public SilverlightMethod BelongsTo
        {
            get
            {
                return belongsTo;
            }
            set
            {
                belongsTo = value;
            }
        }

        private string Descritpion
        {
            get
            {
                return descritpion;
            }
            set
            {
                descritpion = value;
            }
        }


        public string ToJavascript()
        {
            return StringHelper.Format(JavascriptFormats.JavascriptParamCommentFormat, Name, Type, IsInteger.ToString(), Descritpion);
        }

        public override bool Equals(object obj)
        {
            if (obj is SilverlightParameter)
                return this.Name == ((SilverlightParameter)obj).Name
                        && this.Type == ((SilverlightParameter)obj).Type;
            else
                return base.Equals(obj);
        }

        public static bool operator ==(SilverlightParameter x, SilverlightParameter y)
        {
            bool isEqual;
            if (object.ReferenceEquals(x, null))
            {
                if (object.ReferenceEquals(y, null))
                {
                    isEqual = true;
                }
                else
                {
                    isEqual = false;
                }
            }
            else
            {
                isEqual = x.Equals(y);
            }
            return isEqual;
        }

        public static bool operator !=(SilverlightParameter x, SilverlightParameter y)
        {
            return !(x == y);
        }
    }
}